package session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 韩要雷
 *
 * session的作用域---在会话中有效，无论是请求转发还是重定向
 *  request的作用域---在一次会话中有效，只有请求转发有效
 *  设置作用域;setAttribute(name,value)
 *  获取作用域：getAttribute(name)
 *  移除作用域：removeAttribute(name)
 */
@WebServlet("/session02")
public class Session02 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //创建session
        HttpSession session = req.getSession();

        //设置作用域
        session.setAttribute("name","zhangsan");
        //System.out.println(session.getAttribute("name"));
        req.setAttribute("name2","lisi");

        //请求转发
        //req.getRequestDispatcher("session03").forward(req,resp);

        //重定向
        resp.sendRedirect("session03");
    }
}
